package org.dong.Controller;

import com.alibaba.fastjson.JSON;
import org.dong.Dao.UserDao;
import org.dong.Dao.UserDaoImpl;
import org.dong.Pojo.Dto;
import org.dong.Pojo.HttpRequest;
import org.dong.Pojo.HttpResponse;
import org.dong.Tool.Data;
import sun.misc.Request;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class LoginController implements ServerLet {

    @Override
    public void server(HttpRequest request, OutputStream os){

        Dto dto = new Dto();
        String msg = "OK";
        //查找数据中是否存在此用户

        String[] list = request.getRequestData().split("&");


        //获取用户名
        String[] username = list[0].split("=");
        String[] password = list[1].split("=");

        UserDao userDao = new UserDaoImpl();
        Map<String,String> map = new HashMap<>();
        map.put("username",username[1]);
        map.put("password",password[1]);
        if (userDao.checkLogin(map)){
            dto.setData(Data.user);
            dto.setErrMsg("登录成功");
            dto.setStatusCode(2000);

        }else{
            dto.setErrMsg("登录失败");
            dto.setStatusCode(2001);
        }
        msg = JSON.toJSONString(dto);

        HttpResponse response = new HttpResponse(os,request.getUrl(),msg);

    }
}
